package com.hao.tool.web.module.navigation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hao.tool.common.enums.AuthTypeEnum;
import com.hao.tool.web.module.navigation.dto.NavigationDTO;
import com.hao.tool.web.module.navigation.entity.NavigationInfo;
import com.hao.tool.web.module.navigation.mapper.NavigationMapper;
import com.hao.tool.web.module.navigation.service.NavigationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * description
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-21 21:45
 */
@Service
public class NavigationServiceImpl extends ServiceImpl<NavigationMapper, NavigationInfo> implements NavigationService {
    @Override
    public List<NavigationInfo> findByModelId(Integer useType, Long modelId, String userName) {
        QueryWrapper<NavigationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("MODEL_ID", modelId);
        queryWrapper.eq(useType != null, "STATUS", useType);
        queryWrapper.orderByDesc("SORT");

        if (StringUtils.isNotBlank(userName)) {
            queryWrapper
                    .and(wrapper -> wrapper.eq("AUTH_TYPE", AuthTypeEnum.PROTECTED.getCode())
                            .or().eq("AUTH_TYPE", AuthTypeEnum.PUBLIC.getCode())
                            .or(wr -> wr.eq("AUTH_TYPE", AuthTypeEnum.PRIVATE.getCode()).eq("CREATE_USER", userName)));

        } else {
            queryWrapper.and(wrapper -> wrapper.eq("AUTH_TYPE", AuthTypeEnum.PUBLIC.getCode()));
        }
        return this.list(queryWrapper);
    }

    @Override
    public List<NavigationInfo> findByInfo(Integer useType, Long modelId, String linkName) {
        QueryWrapper<NavigationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(modelId != null, "MODEL_ID", modelId);
        queryWrapper.eq(useType != null, "STATUS", useType);
        queryWrapper.like(StringUtils.isNotBlank(linkName), "LINK_NAME", linkName);
        queryWrapper.ne("AUTH_TYPE", -1);

        queryWrapper.orderByDesc("SORT");
        return this.list(queryWrapper);
    }

    @Override
    public void incrementClickCount(Long linkId) {
        NavigationInfo info = this.getById(linkId);
        UpdateWrapper<NavigationInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", info.getId());
        updateWrapper.set("CLICK_COUNT", info.getClickCount() + 1);
        update(updateWrapper);
    }

    @Override
    public NavigationInfo addNavigation(NavigationDTO dto, String userName) {
        NavigationInfo info = new NavigationInfo();

        info.setId(null)
                .setModelId(dto.getModelId())
                .setAuthType(dto.getAuthType())
                .setLinkName(dto.getLinkName())
                .setLinkUrl(dto.getLinkUrl())
                .setCreatUser(userName)
                .setCreateTime(LocalDateTime.now())
                .setLogoUrl(dto.getLogoUrl())
                .setBtnClass(dto.getBtnClass())
                .setClickCount(0L)
                .setContent(dto.getContent())
                .setSort(dto.getSort())
                .setLinkClass(dto.getLinkClass())
                .setStatus(dto.getStatus())
                .setTagGroup(dto.getTagGroup())
                .setModelName(dto.getLinkName())
                .setUserAuth(dto.getUserAuth())
                .setNavigationType(dto.getNavigationType());
        this.save(info);
        return info;
    }
}
